package com.ruoyi.mongo.cet4.service.impl;

import com.ruoyi.mongo.cet4.Entity4;
import com.ruoyi.mongo.cet4.EntityExpand4;
import com.ruoyi.mongo.cet4.EntityTestRecord;
import com.ruoyi.mongo.cet4.service.EntityInterface;
import com.ruoyi.mongo.cet4.service.EntityNetInterface;
import com.ruoyi.common.utils.DataUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mongo.service.MongoService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

@Service
public class EntityService implements EntityInterface {

    private final MongoService mongoService;

    private final EntityNetInterface entityNetInterface;

    public EntityService(MongoService mongoService, EntityNetInterface entityNetInterface) {
        this.mongoService = mongoService;
        this.entityNetInterface = entityNetInterface;
    }

    @Override
    public void saveData(Entity4 entity) {
        Long userId = getUserId();
        saveData(entity, userId);
    }

    @Override
    public void saveData(Entity4 entity, Long userId) {
        entity.setTime(DateUtils.getLocalTime());
        entityNetInterface.insertNetDataByUser(entity, userId);
        entity.set_id(null);
        entity.setWordMore(null);
        mongoService.saveToSecondary(entity,userId);
    }

    @Override
    public void saveTestData(EntityTestRecord entity, Long userId) {
        entity.set_id(null);
        mongoService.saveTestDataList(entity,userId);
    }

    @Override
    public void saveTestList(List<EntityTestRecord> list){
        Long userId = getUserId();
        list.forEach(entity->{
            saveTestData(entity, userId);
        });
    }

    @Override
    public void saveList(List<Entity4> list){
        Long userId = getUserId();
        list.forEach(entity->{
           saveData(entity, userId);
        });
    }

    @Override
    public List<Entity4> selectListByPracticeId(String practice){
        Long userId = getUserId();
        return selectListByPracticeId(practice,userId);
    }

    @Override
    public List<EntityTestRecord> selectTestListByPracticeId(String practice){
        Long userId = getUserId();
        return selectTestListByPracticeId(practice,userId);
    }

    @Override
    public List<EntityTestRecord> selectTestListByPracticeId(String practice, long userId) {
        return mongoService.selectTestByPractice(practice,userId);
    }

    @Override
    public List<Entity4> selectListByPracticeId(String practice, long userId) {
        return mongoService.selectByPractice(practice,userId);
    }

    @Override
    public List<EntityExpand4> selectListByType(String type, int limit) {
        return mongoService.selectByType(type,limit);
    }

    @Override
    public List<EntityExpand4> selectListByGroup(Integer group){
        List<EntityExpand4> list = mongoService.selectByGroup(group);
        return DataUtils.selectRandomByNumList(list);
    }

    @Override
    public List<EntityExpand4> selectListByNotGroup(Integer group){
        List<EntityExpand4> list = mongoService.selectByNotGroup(group);
        return DataUtils.selectRandomByNumList(list);
    }

    @Override
    public List<EntityExpand4> selectRandomNotGroup(Integer group){
        List<EntityExpand4> list = mongoService.selectNotGroupRandomData(group);
        return DataUtils.selectRandomByNumList(list);
    }

    @Override
    public List<EntityExpand4> selectListByTypeRandom(String type, int limit) {
        return mongoService.selectByTypeRandom(type,limit);
    }
}
